package fr.lelouet.constraints.integers;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**
 * Integer Domain of a variable:
 * <ul>
 * <li>iterable over the Integers it contains</li>
 * <li>has a max and min value</li>
 * <li>knows if it contains an element</li>
 * </ul>
 * 
 * @author admin
 * 
 */
public interface Domain extends Iterable<Integer> {

    public Integer max();

    public Integer min();

    public boolean contains(Integer val);

    public static final Domain N = new Domain() {

	@Override
	public Iterator<Integer> iterator() {
	    return new Iterator<Integer>() {

		boolean hasNext = true;
		int nextVal = Integer.MIN_VALUE;

		@Override
		public boolean hasNext() {
		    return hasNext;
		}

		@Override
		public Integer next() {
		    if (!hasNext()) {
			throw new NoSuchElementException();
		    }
		    int ret = nextVal;
		    if (ret == Integer.MAX_VALUE) {
			hasNext = false;
		    } else {
			nextVal++;
		    }
		    return ret;
		}

		@Override
		public void remove() {
		    throw new UnsupportedOperationException();
		}
	    };
	}

	@Override
	public Integer min() {
	    return Integer.MIN_VALUE;
	}

	@Override
	public Integer max() {
	    return Integer.MAX_VALUE;
	}

	@Override
	public boolean contains(Integer val) {
	    return true;
	}
    };

}
